package cn.itcast.mybatis;

import java.sql.*;

/**
 * Created by Administrator on 2017/9/3.
 */
public class JDBCTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/mybatis-903";
            String user = "root";
            String password = "123";
            connection = DriverManager.getConnection(url,user,password);
            String sql = "select * from tb_user where id =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1,2l);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getString("user_name"));
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getInt("age"));
            }
        }finally {
            if (resultSet!=null){
                resultSet.close();
            }
            if (preparedStatement!=null){
                preparedStatement.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
    }
}
